home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_054 / ispell / tree.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  184 lines

  1. /* -*- Mode:Text -*- */
  2. /*
  3.  * tree.c - a tree style dictionary for user's personal words
  4.  *
  5.  * Pace Willisson, 1983
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "ispell.h"
  11.  
  12. char *getenv();
  13. char *upcase();
  14.  
  15. static struct node *root = NULL;
  16. struct node *tinsert();
  17.  
  18. static char personaldict[100];
  19. static FILE *dictf;
  20. static newwords = 0;
  21.  
  22. treeinit ()
  23. {
  24.     char *p;
  25.     char buf[BUFSIZ];
  26.  
  27.     p = getenv ("HOME");
  28.     if (p == NULL)
  29.         return;
  30.  
  31.     strcpy (personaldict, p);
  32.     strcat (personaldict, "/ispell.words");
  33.  
  34.     if ((dictf = fopen (personaldict, "r")) == NULL)
  35.         return;
  36.  
  37.     while (fgets (buf, sizeof buf, dictf) != NULL) {
  38.         int len = strlen (buf) - 1;
  39.  
  40.         if (buf [ len ] == '\n')
  41.             buf [ len ] = 0;
  42.         treeinsert (buf, 1);
  43.     }
  44.  
  45.     fclose (dictf);
  46.  
  47.     newwords = 0;
  48.  
  49.     if (!lflag && !aflag && access (personaldict, 2) < 0)
  50.         printf ("Warning: Cannot update personal dictionary (%s)\r\n", personaldict);
  51. }
  52.  
  53. treeprint ()
  54. {
  55.     printf ("(");
  56.     tprint (root);
  57.     printf (")");
  58. }
  59.  
  60. static
  61. tprint (root)
  62. struct node *root;
  63. {
  64.     if (root == NULL)
  65.         return;
  66.     printf ("%s ", root->word);
  67.     tprint (root->left);
  68.     tprint (root->right);
  69. }
  70.  
  71.  
  72. treeinsert (word, keep)
  73. char *word;
  74. {
  75.     char nword[BUFSIZ];
  76.     strcpy (nword, word);
  77.     root = tinsert (upcase (nword), root, keep);
  78.     newwords = newwords || keep;
  79. }
  80.  
  81. static
  82. struct node *
  83. tinsert (word, root, keep)
  84. char *word;
  85. struct node *root;
  86. {
  87.     int cmp;
  88.  
  89.     if (root == NULL) {
  90.         root = (struct node *) calloc (1, sizeof (struct node));
  91.         root->word = (char *) malloc (strlen (word) + 1);
  92.         strcpy (root->word, word);
  93.         root->keep = keep;
  94.         return (root);
  95.     }
  96.  
  97.     cmp = strcmp (word, root->word);
  98.  
  99.     if (cmp == 0)
  100.         return (root);
  101.  
  102.     if (cmp < 0)
  103.         root->left = tinsert (word, root->left, keep);
  104.     else
  105.         root->right = tinsert (word, root->right, keep);
  106.  
  107.     return (root);
  108. }
  109.  
  110. treelookup (word)
  111. char *word;
  112. {
  113.     char nword[BUFSIZ];
  114.     strcpy (nword, word);
  115.     if (tlookup (upcase (nword), root)) {
  116.         return (1);
  117.     }
  118.     return (0);
  119. }
  120.  
  121. static
  122. tlookup (word, root)
  123. char *word;
  124. struct node *root;
  125. {
  126.     int cmp;
  127.  
  128.     if (root == NULL)
  129.         return (0);
  130.  
  131.     cmp = strcmp (word, root->word);
  132.  
  133.     if (cmp == 0)
  134.         return (1);
  135.  
  136.     if (cmp < 0)
  137.         return (tlookup (word, root->left));
  138.     else
  139.         return (tlookup (word, root->right));
  140. }
  141.  
  142. treeoutput ()
  143. {
  144.     if (newwords == 0)
  145.         return;
  146.  
  147.     if ((dictf = fopen (personaldict, "w")) == NULL) {
  148.         fprintf (stderr, "Can't create %s\r\n", personaldict);
  149.         return;
  150.     }
  151.  
  152.     toutput1 (root);
  153.     newwords = 0;
  154.     fclose (dictf);
  155. }
  156.  
  157. static
  158. toutput1 (root)
  159. struct node *root;
  160. {
  161.     if (root == NULL)
  162.         return;
  163.  
  164.     if (root->keep)
  165.         fprintf (dictf, "%s\n", root->word);
  166.  
  167.     toutput1 (root->left);
  168.     toutput1 (root->right);
  169. }
  170.  
  171. char *
  172. upcase (s)
  173. register char *s;
  174. {
  175.     register char *os = s;
  176.  
  177.     while (*s) {
  178.         if (islower (*s))
  179.             *s = toupper (*s);
  180.         s++;
  181.     }
  182.     return (os);
  183. }
  184.